//
// Created by hunan on 25-4-4.
//
#include "flv_message.h"
#include "./../amf_module/amf.h"

FlvMessage::FlvMessage()
{
    m_metadata.m_object = nullptr;
}

FlvMessage::~FlvMessage()
{
    m_tag_vec.clear();
}

void FlvMessage::FlvHeader(const struct FlvMessage::FlvHeader & flv_header)
{
    m_header = flv_header;
}

void FlvMessage::PushTag(struct FlvMessage::FlvTag & tag)
{
    m_tag_vec.push_back(tag);
}

struct FlvMessage::FlvHeader FlvMessage::GetFlvHeader()
{
    return m_header;
}

struct FlvMessage::FlvTag * FlvMessage::GetFlvTagByIndex(int index)
{
    if (static_cast<int>(m_tag_vec.size()) > index && index > 0)
    {
        return &m_tag_vec[index - 1];
    }
    return nullptr;
}

int FlvMessage::FlvTagCount() const
{
    return (int)m_tag_vec.size();
}

void FlvMessage::RemoveByIndex(int index)
{
    if (static_cast<int>(m_tag_vec.size()) >= index && index > 0)
    {
        m_tag_vec.erase(m_tag_vec.begin() + index - 1);
    }
}

void FlvMessage::MetaData(AmfArray * object)
{
    if (m_metadata.m_object)
    {
        delete m_metadata.m_object;
        m_metadata.m_object = nullptr;
    }
    m_metadata.m_object = object;
    for (int index = 0; index < m_metadata.m_object->GetArraySize(); index++)
    {
        //printf("key:%s,value:%s.\n", m_metadata.m_object->KeyByIndex(index).c_str(), m_metadata.m_object->ValueByIndex(index)->AmfValueString().c_str());
    }
}

struct FlvMessage::FlvMetaData * FlvMessage::GetMetaData()
{
    return &m_metadata;
}
